import ViewContainer from "./view_container";
import ButtonState from "./button_state";
import EventCenter from "./event_center";
import TouchEvent from "./touch_event";
import Event from "./event";

export default class Button extends ViewContainer {
  normalStateView = null;
  clickStateView = null;
  disableStateView = null;
  clone() {
    let e = this,
      t = new Button();
    return e.copyProperties(t), t;
  }
  copyProperties(t) {
    let n = this;
    ViewContainer.prototype.copyProperties.call(n, t),
      t.initButton(),
      t.switchMode(n.switchMode());
  }
  dealloc() {
    let t = this;
    EventCenter.removeListener(t), ViewContainer.prototype.dealloc.call(t);
  }
  updatePropsFromXML(t) {
    let n = this;
    ViewContainer.prototype.updatePropsFromXML.call(n, t),
      n.initButton(),
      t.is("[switch-mode]") && n.switchMode(t.attr("switch-mode") == "true");
  }
  initButton() {
    let e = this;
    (e.normalStateView = e.subViewByName("normal")),
      (e.clickStateView = e.subViewByName("click")),
      (e.disableStateView = e.subViewByName("disable")),
      e.updateButtonView(),
      EventCenter.addListener(e, e, TouchEvent.TOUCH_START, e.onButtonTouchStart),
      EventCenter.addListener(e, e, TouchEvent.TOUCH_END, e.onButtonTouchEnd);
  }
  onButtonTouchStart() {
    let e = this;
    e.switchMode() === !1 && e.buttonState(ButtonState.CLICK_STATE);
  }
  onButtonTouchEnd() {
    let e = this;
    e.switchMode() === !1
      ? e.buttonState(ButtonState.NORMAL_STATE)
      : (e.buttonState() == ButtonState.NORMAL_STATE
          ? e.buttonState(ButtonState.CLICK_STATE)
          : e.buttonState(ButtonState.NORMAL_STATE),
        EventCenter.dispatchEvent(new Event(e, Event.CHANGE)));
  }
  _switchMode = !1;
  switchMode(e?) {
    let n = this;
    return (
      typeof e == "boolean" &&
        e != n._switchMode &&
        ((n._switchMode = e), n.buttonState(ButtonState.NORMAL_STATE)),
      n._switchMode
    );
  }
  _buttonState = ButtonState.NORMAL_STATE;
  buttonState(e?) {
    let t = this;
    return (
      isNaN(e) === !1 && ((t._buttonState = e), t.updateButtonView()),
      t._buttonState
    );
  }
  enabled(t?) {
    let n = this,
      r = ViewContainer.prototype.enabled.call(n, t);
    return typeof t == "boolean" && n.updateButtonView(), r;
  }
  updateButtonView() {
    let e = this;
    e.switchMode()
      ? (e.disableStateView != null && e.disableStateView.visible(!1),
        e.normalStateView != null &&
          e.normalStateView.visible(e.buttonState() == ButtonState.NORMAL_STATE),
        e.clickStateView != null &&
          e.clickStateView.visible(e.buttonState() == ButtonState.CLICK_STATE))
      : (e.disableStateView != null &&
          e.disableStateView.visible(e.enabled() == 0),
        e.normalStateView != null &&
          (e.enabled()
            ? e.normalStateView.visible(e.buttonState() == ButtonState.NORMAL_STATE)
            : e.normalStateView.visible(e.disableStateView == null)),
        e.clickStateView != null &&
          e.clickStateView.visible(
            e.enabled() && e.buttonState() == ButtonState.CLICK_STATE
          ));
  }
}
